package com.ruoyi.bmxt.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bmxt.domain.ui.FileZipDto;
import com.ruoyi.bmxt.service.IBmStudentService;
import com.ruoyi.bmxt.utils.LoginUserUtils;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bmxt.domain.BmFiles;
import com.ruoyi.bmxt.service.IBmFilesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件资料管理Controller
 * 
 * @author 魏建波
 * @date 2023-02-24
 */
@RestController
@RequestMapping("/bmxt/files")
public class BmFilesController extends BaseController
{
    @Autowired
    private IBmFilesService bmFilesService;

    @Autowired
    private IBmStudentService bmStudentService;

    /**
     * 查询文件资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('bmxt:files:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmFiles bmFiles)
    {
        startPage();
        List<BmFiles> list = bmFilesService.selectBmFilesList(bmFiles);
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public AjaxResult allList(BmFiles bmFiles)
    {
        List<BmFiles> list = bmFilesService.selectBmFilesList(bmFiles);
        return success(list);
    }
    /**
     * 导出文件资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('bmxt:files:export')")
    @Log(title = "文件资料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmFiles bmFiles)
    {
        List<BmFiles> list = bmFilesService.selectBmFilesList(bmFiles);
        ExcelUtil<BmFiles> util = new ExcelUtil<BmFiles>(BmFiles.class);
        util.exportExcel(response, list, "文件资料管理数据");
    }

    /**
     * 获取文件资料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('bmxt:files:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(bmFilesService.selectBmFilesByFileId(fileId));
    }

    /**
     * 新增文件资料管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:files:add')")
    @Log(title = "文件资料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmFiles bmFiles)
    {
        return toAjax(bmFilesService.insertBmFiles(bmFiles));
    }

    /**
     * 文件资料更新
     * @param bmFiles
     * @return
     * @throws IOException
     * @throws InvalidExtensionException
     */
    @PostMapping("/addStudentFileUpload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId", value = "学生Id", dataType = "String", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "fileTypeId", value = "文件类型", dataType = "String", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "files", value = "上传文件列表", dataType = "MultipartFile", dataTypeClass = MultipartFile.class, required = true),
    })
    public AjaxResult addStudentFileUpload(BmFiles bmFiles) throws IOException, InvalidExtensionException {
        return success(bmFilesService.addStudentFileUpload(bmFiles));
    }

    /**
     * 修改文件资料管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:files:edit')")
    @Log(title = "文件资料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmFiles bmFiles)
    {
        return toAjax(bmFilesService.updateBmFiles(bmFiles));
    }

    /**
     * 删除文件资料管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:files:remove')")
    @Log(title = "文件资料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(bmFilesService.deleteBmFilesByFileIds(fileIds));
    }

    //  下载zip文件
    @PostMapping("/downloadZipFile")
    public ResponseEntity<InputStreamResource> downloadFile(FileZipDto fileZipDto)
            throws Exception {
        String zipPath = bmStudentService.createFileByUser(fileZipDto);
        FileSystemResource file = new FileSystemResource(zipPath);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }
}
